package com.huatai.exchange.utils;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.dal.DBUtils;
import com.faneui.service.Dbconfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Service
public class GzbUtils   {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public String checkGzbIsFinish(String ztbh, String occurDate) throws BusinessException {
        logger.info("校验估值表是否生成");
        Connection conn = null;
        String user = "facx";
        String r;
        try {
            conn = DBUtils.getConnection(Dbconfig.FATADB, user, Dbconfig.getPasswordByUserId(user));
            String sql = " select 1 from hsfa.vjk_wbfk_gzb where l_sfqr = 1 and l_ztbh = ? and d_ywrq = to_date(?,'yyyymmdd') ";
            List<Map<String, Object>> result = DBUtils.executeQuery(conn, sql, ztbh, occurDate);
            r = ListUtils.isEmpty(result) ? "估值表未生成" : "1";
        } catch (Exception ex) {
            logger.error("", ex);
            r = ex.getMessage();
        } finally {
            DBUtils.closeAll(conn);
        }
        logger.info("校验估值表是否生成");
        return r;
    }

    public List<Map<String, Object>> getFundNavList(String ztbh, String startDate, String endDate) throws BusinessException {
        if (StringUtils.isBlank(startDate)) {
            startDate = "20000101"; // 从两千年开始
        }
        Connection conn = null;
        String user = "facx";
        List<Map<String, Object>> result = null;
        try {
            conn = DBUtils.getConnection(Dbconfig.FATADB, user, Dbconfig.getPasswordByUserId(user));
            StringBuffer sql = new StringBuffer(" select to_char(d_ywrq, 'yyyy-mm-dd') d_date,                                                                \n") ;
            sql.append("        to_char(sum(decode(vc_kmdm, '今日单位净值:', to_number(vc_kmmc), 0)),'0.0000') f_netvalue,           \n");
            sql.append("        to_char(sum(decode(vc_kmdm, '今日单位净值:', to_number(vc_kmmc), 0)),'0.0000') f_totalnetvalue       \n");
            sql.append("   from hsfa.vjk_wbfk_gzb t                                                                                       \n");
            sql.append("  where l_sfqr = 1 and t.l_ztbh = ?  and d_ywrq >= to_date(?,'yyyymmdd')                                     \n");
            sql.append("    and d_ywrq <= to_date(?,'yyyymmdd') and vc_kmdm in ('今日单位净值:', '累计单位净值:')                    \n");
            sql.append("  group by d_ywrq                                                                                            \n");
            sql.append("  order by d_ywrq desc ");
            result = DBUtils.executeQuery(conn, sql.toString(), ztbh, startDate, endDate);
        } catch (Exception ex) {
            logger.error("", ex);
        } finally {
            DBUtils.closeAll(conn);
        }
        return result;
    }
}
